package com.xyy.saas.payment.merchant.core.dto;

import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.util.AmountCastUtil;
import lombok.Builder;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Data
@Builder
public class QueryAccountBalanceVo {
    /**
     * 总行数
     */
    private String totalNum;
    /**
     * 本次返回行数
     */
    private String resultNum;
    /**
     * 结束标识
     * 0 否
     * 1 是
     */
    private String endFlag;
    /**
     * 账户明细
     */
    private List<QueryAccountBalanceItem> accountItems;

    /**
     * 解析明细
     */
    public static List<QueryAccountBalanceItem> parseItems(List<Map<String, String>> param) {
        if(param == null) {
            return null;
        }

        List<QueryAccountBalanceItem> items = new ArrayList<>();

        for(Map<String, String> item : param) {
            items.add(QueryAccountBalanceItem.builder().
                    accountId(item.get("TranNetMemberCode")).
                    thirdUserId(item.get("SubAcctNo")).
                    accountType(item.get("SubAcctProperty")).
                    accountName(item.get("SubAcctName")).
                    availableBalance(AmountCastUtil.castY2F(item.get("AcctAvailBal"))).
                    withdrawBalance(AmountCastUtil.castY2F(item.get("CashAmt"))).
                    modifyDate(item.get("MaintenanceDate")).
                    build());
        }

        return items;
    }

    /**
     * 可用余额
     */
    public Long getSingleAvailableBalance(String accountId) throws PaymentException {
        if(CollectionUtils.isEmpty(accountItems)) {
            throw new PaymentException("Failed to query balance.");
        }

        QueryAccountBalanceItem balanceItem = null;

        for(QueryAccountBalanceItem item : accountItems) {
            if(Objects.equals(accountId, item.getAccountId())) {
                balanceItem = item;
                break;
            }
        }

        if(balanceItem == null) {
            throw new PaymentException("No data found.");
        }

        return balanceItem.getAvailableBalance();
    }

    /**
     * 可提现余额
     */
    public Long getSingleWithdrawBalance(String accountId) throws PaymentException {
        if(CollectionUtils.isEmpty(accountItems)) {
            throw new PaymentException("Failed to query balance.");
        }

        QueryAccountBalanceItem balanceItem = null;

        for(QueryAccountBalanceItem item : accountItems) {
            if(Objects.equals(accountId, item.getAccountId())) {
                balanceItem = item;
                break;
            }
        }

        if(balanceItem == null) {
            throw new PaymentException("No data found.");
        }

        return balanceItem.getWithdrawBalance();
    }
}

@Data
@Builder
class QueryAccountBalanceItem {
    /**
     * 支付平台商户标识
     */
    private String accountId;
    /**
     * 账户
     */
    private String thirdUserId;
    /**
     * 账户类型
     * 1 普通会员子账号
     * 2 挂账子账号
     * 3 手续费子账号
     * 4 利息子账号
     * 5 平台担保子账号
     * 7 平台在途子账户
     * 11 汇总影子子账户
     * 12 提现在途子账户
     * 13 营销子账户
     * 22 特殊挂账子账户（22类型如没有返回，则表示没有）
     */
    private String accountType;
    /**
     * 账户名称
     */
    private String accountName;
    /**
     * 可用余额
     */
    private Long availableBalance;
    /**
     * 可提现余额
     */
    private Long withdrawBalance;
    /**
     * 创建/修改日志
     */
    private String modifyDate;
}